/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package numberporting.service;

import java.util.Enumeration;
import java.util.Vector;
import javax.jws.WebService;
import numberporting.bean.Customer;
import numberporting.bean.CustomerStore;
import numberporting.bean.PortingRequest;
import numberporting.bean.PortingResult;

/**
 *
 * @author Thomas
 */
@WebService(serviceName="NumberPortingService")
public class NumberPortingService implements INumberPortingService{
    
    private CustomerStore customerStore;
    private Vector<PortingRequest> portingQueue;

    public NumberPortingService(CustomerStore theCustomerStore, PortingQueue thePortingQueue)
    {
        this.customerStore = theCustomerStore;
        this.portingQueue = thePortingQueue;
    }
    public boolean check_porting_possible(Customer theCustomer, int ProviderID) throws InvalidCustomerException {
        
        customerStore.checkCustomer(theCustomer.getId());
        
        if(theCustomer.getName().equals("Illegal User"))
        {
            return false;
        }
        return true;
    }

    public void schedule_batch_porting(PortingRequest[] PortingRequests) throws InvalidCustomerException {
        
        System.out.println("+++ put somthing on PortingQueue +++");
        for(int i = 0; i<PortingRequests.length;i++)
        {
            customerStore.checkCustomer(PortingRequests[i].getCustomer().getId());
            PortingRequests[i].setCustomer(customerStore.getCustomer(PortingRequests[i].getCustomer().getId()));
            portingQueue.add(PortingRequests[i]);
        }
        
    }

    public boolean check_porting_status(Customer theCustomer) throws InvalidCustomerException {
        
        customerStore.checkCustomer(theCustomer.getId());
        for(Enumeration<PortingRequest> e = portingQueue.elements();e.hasMoreElements();)
        {
            if(e.nextElement().getCustomer().getId().equals(theCustomer.getId()))
            {
                return false;
            }
        }
        return true;
    }

    public PortingResult get_porting_result(Customer theCustomer) throws NotFinishedException, InvalidCustomerException {
       
        customerStore.checkCustomer(theCustomer.getId());
        if(!check_porting_status(theCustomer)) throw new NotFinishedException("Not finished yet!");

        Vector<PortingResult> tmp = customerStore.getCustomer(theCustomer.getId()).getPortingResults();
        
        return tmp.lastElement();
        
    }

}
